package com.wectrls.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.system.ApplicationHome;

import java.io.File;
import java.util.Calendar;

/**
 * @author chenjian
 * @Package
 * @Description:
 * @date 2019/7/17 16:58
 */
@Slf4j
public class RecordPathUtils {

    /**
     * @param aClass
     * @param filePrefix
     * @return
     */
    public static String getRecordPath(Class<? extends Object> aClass, String filePrefix) {
        ApplicationHome home = new ApplicationHome(aClass);
        String parent = home.getSource().getParent();
        String dir = parent + "/record/" + filePrefix + "/";
        File file = new File(dir);
        if (!file.isDirectory()) {
            boolean md = file.mkdirs();
            if (md) {
                log.debug("创建记录数据序列化文件夹");
            } else {
                log.warn("警告！创建记录数据序列化文件夹异常");
            }
        }
        //创建对应的文件
        Calendar instance = Calendar.getInstance();
        int year = instance.get(Calendar.YEAR);
        int month = instance.get(Calendar.MONTH) + 1;
        String fileName = getFileName(year, month, filePrefix);
        String filePath = dir + fileName;
        return filePath;
    }

    //根据年月和文件前缀名查找对应的文件
    public static String getFileName(Integer year, Integer month, String filePrefix) {
        String recordFile = filePrefix + "-" + year + "-" + month + ".txt";
        return recordFile;
    }
}
